package Leetcode;

import java.util.Arrays;

/**
 * 给你一个整数数组 nums，请你将该数组升序排列。
 * 输入：nums = [5,1,1,2,0,0]
 * 输出：[0,0,1,1,2,5]
 */
public class 力扣912_排序数组 {

    public static void main(String[] args) {
        int[] nums = new int[]{5, 1, 1, 2, 0, 0};
        sortArray(nums);
        System.out.println(Arrays.toString(nums));
    }

    static int[] sortArray(int[] nums) {
        sortArray(nums, 0, nums.length - 1);
        return nums;
    }

    static void sortArray(int[] nums, int l, int r) {
        if (l < r) {
            int less = l;
            int more = r - 1;
            while (less <= more) {
                if (nums[less] > nums[r]) {
                    swapNum(nums, less, more);
                    more--;
                } else {
                    less++;
                }
            }
            swapNum(nums, more + 1, r);
            sortArray(nums, more + 1, r);
            sortArray(nums, l, more);
        }
    }

    static void sortArray2(int[] nums, int l, int r) {
        if (l < r) {
            int less = l + 1;
            int more = r;
            while (less <= more) {
                if (nums[less] > nums[l]) {
                    swapNum(nums, less, more);
                    more--;
                } else {
                    less++;
                }
            }
            swapNum(nums, l, less - 1);
            sortArray2(nums, l, less - 1);
            sortArray2(nums, less, r);
        }
    }

    static void swapNum(int[] nums, int l, int r) {
        int num = nums[l];
        nums[l] = nums[r];
        nums[r] = num;
    }

}
